/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hue;

import java.io.ByteArrayOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Hue
 */
public class PhpServlet extends OpenURL {
    
    @Override
    public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        super.process(request, response);
        
        String requestURI = request.getRequestURI();
        if(request.getQueryString() != null) {
             requestURI += "?" + request.getQueryString();
        }
        String url = SERVER_URL + requestURI;

        ByteArrayOutputStream baOut = new ByteArrayOutputStream();
        if("POST".equals(request.getMethod())) {
            httpPost(request, response, url, baOut);
        }
        else {
            httpGet(request, response, url, baOut);
        }
        String content = baOut.toString();
        baOut.close();

        String src = "src=\"";
        int iIndex = content.indexOf(src);
        if(iIndex != -1) {
            int iEnd = content.indexOf("\"", iIndex + src.length() + 1);
            String iframeUrl = content.substring(iIndex + src.length(), iEnd);
            content = replace(content, iframeUrl, "loadFrame?url=" + iframeUrl);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(content).append("\r\n");
        sb.append("<script>setTimeout('parent.clickSkipAdButton()', 8000);</script>");
        response.getOutputStream().write(sb.toString().getBytes());
    }
}
